package com.compty.paint.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.compty.paint.domain.ImageInfo;
import com.compty.paint.domain.Painter;
import com.compty.paint.domain.PhotoData;


public class PainterService {

	/**
	 * 查询所有画家
	 */
	public List<Painter> getAll() {
		Connection conn = DBUtil.getConnection();
		try {
			List<Painter>  returnList = new ArrayList<Painter>();
			String sql = "select * from t_painter ";
			PreparedStatement statement = conn.prepareStatement(sql);
			ResultSet rs = statement.executeQuery();
			while(rs.next()){
				Painter painter = new Painter();
				Long image_id = rs.getLong("image_id");
				painter.setImageInfo(findImageinfoById(conn,image_id));
				
				returnList.add(painter);
			}
			
			return returnList;
		} catch (SQLException e1) {
			e1.printStackTrace();
		}finally{
			if(conn != null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	
	private void buildPainter(Painter painter, ResultSet rs) {
		try {
			painter.setId(rs.getLong("id"));
			painter.setName(rs.getString("name"));
			painter.setAge(rs.getString("age"));
			painter.setGraduatedFrom(rs.getString("graduated"));
			painter.setHonour(rs.getString("honour"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * 查询所有名家
	 */
	public List<Painter> getAllPainterStar() {
		Connection conn = DBUtil.getConnection();
		try {
			List<Painter>  returnList = new ArrayList<Painter>();
			String sql = "select * from t_painter where type=2";
			PreparedStatement statement = conn.prepareStatement(sql);
			ResultSet rs = statement.executeQuery();
			while(rs.next()){
				Painter painter = new Painter();
				
				Long image_id = rs.getLong("image_id");
				painter.setImageInfo(findImageinfoById(conn,image_id));
				
				returnList.add(painter);
			}
			return returnList;
		} catch (SQLException e1) {
			e1.printStackTrace();
		}finally{
			if(conn != null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		
		return null;
	}
	
	public ImageInfo findImageinfoById(Connection conn,Long id){
		String sql = "select * from t_imageinfo where id=? ";
		try {
			PreparedStatement imagestatement = conn.prepareStatement(sql);
			imagestatement.setLong(1, id);
			ResultSet rs = imagestatement.executeQuery();
			while(rs.next()){
				ImageInfo imageInfo = new ImageInfo();
				imageInfo.setId(rs.getLong("id"));
				imageInfo.setOriginalName(rs.getString("originalName"));
				
				Long photo_data_id = rs.getLong("photo_data_id");
				PhotoData photoData = findPhotoDataById(conn, photo_data_id);
				imageInfo.setPhoto(photoData);
				
				imageInfo.setSharpId(rs.getLong("sharp_id"));
				imageInfo.setSuffix(rs.getString("suffix"));
				imageInfo.setNewNameUploaded(rs.getString("newname_uploaded"));
				imageInfo.setMemo(rs.getString("memo"));
				return imageInfo;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public PhotoData findPhotoDataById(Connection conn,Long id){
		String sql = "select * from t_imageinfo_data where id=?";
		try {
			PreparedStatement photostatement = conn.prepareStatement(sql);
			photostatement.setLong(1, id);
			ResultSet rs = photostatement.executeQuery();
			while(rs.next()){
				PhotoData photoData = new PhotoData();
				photoData.setId(rs.getLong("id"));
				java.sql.Blob blob = rs.getBlob("data");  
			    InputStream ins = blob.getBinaryStream(); 
			    ByteArrayOutputStream baos = new ByteArrayOutputStream();
		        byte[] b = new byte[1024];  
		        int len = 0;  
		        while ( (len = ins.read(b)) != -1) {  
		        	baos.write(b, 0, len);  
		        }  
				photoData.setData(baos.toByteArray());
				baos.flush();
				baos.close();  
			    ins.close();
				return photoData;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
